<?php
/**
 *用户登录处理页面
 * User: wuz
 * Date: 2016/8/23
 * Time: 12:07
 */

/*
判断输入是否为空
判断两次输入密码是否完全相等
判断用户是否已经存在
md5对密码进行加密
判断用户注册是否成功
 */
header("content-type:text/html;charset=utf-8");
include "./Mysql/MySqlClass.php";
include "FileUpload/FileUpload.php";
$up = new FileUpload;
$up->set("path", "./images/");
$up->set("maxsize", 2000000);
$up->set("allowtype", array("gif", "png", "jpg", "jpeg"));
$up->set("israndname", true);
$up->upload("file");
$pictureName = $up->getFileName();
$picturetDir = "images/" . $pictureName;
$mySQL       = new Ycf\Lession\MySqlClass\MySqlClass('test_db', 'root', '123456');
if (empty($_POST['userName']) || empty($_POST['password'])) {
    echo "<script>alert('用户名和密码不能为空'); location.href='index.html';</script>";
} else if ($_POST['password'] !== $_POST['rePassword']) {
    echo "<script>alert('两次输入密码不一致，清重新输入'); location.href='index.html';</script>";
} else {
    $password       = md5($_POST['password']); //md5对密码加密
    $selectUserName = array('userName' => $_POST['userName'],
        'password'                         => $password);
    $user = $mySQL->select("html_admin", $selectUserName); //通过用户名和密码查询数据库
    if (count($user) != 0) { //返回的数组不为空则进入系统
        echo "<script>alert('用户已存在，请重新输入'); location.href='index.html';</script>";
    } else {
        $hobby   = implode(".", $_POST['hobby']);
        $newUser = array('userName' => $_POST['userName'],
            'password'                  => $password,
            'gender'                    => $_POST['sex'],
            'hobby'                     => $hobby,
            'city'                      => $_POST['city'],
            'picture'                   => $picturetDir,
            'personIntroduct'           => $_POST['content'],
        );
        $singUp = $mySQL->insert("html_admin", $newUser);
        if (!empty($singUp)) {
            echo "<script>alert('注册成功'); location.href='index.html';</script>";
        }
    }
}
